package com.crop.back.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crop.back.common.enums.Deleted;
import com.crop.back.common.utils.ResultBean;
import com.crop.back.dto.ProductCategoryDTO;
import com.crop.back.dto.ProductDTO;
import com.crop.back.entity.Product;
import com.crop.back.entity.ProductCategory;
import com.crop.back.entity.Supplier;
import com.crop.back.service.ProductCategoryService;
import com.crop.back.service.SupplierService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.annotation.Resource;

/**
 * <p>
 * 产品分类表 前端控制器
 * </p>
 *
 * @author 作者
 * @since 2025-01-20
 */
@RestController
@RequestMapping("/product-category")
public class ProductCategoryController {

    @Resource
    private ProductCategoryService categoryService;

    @GetMapping("/name")
    public ResultBean getName() {
        List<ProductCategory> list = categoryService.list(new LambdaQueryWrapper<ProductCategory>()
                .eq(ProductCategory::getDeleted, Deleted.NOT_DELETED.getCode()));
        return ResultBean.ok(list);
    }

    @PostMapping("/list")
    public ResultBean getAll(@RequestBody ProductCategoryDTO productCategoryDTO) {
        Integer page = productCategoryDTO.getPage();
        Integer pageSize = productCategoryDTO.getPageSize();
        Page<ProductCategory> pageInfo = new Page<>(page, pageSize);
        Page<ProductCategory> res = categoryService.page(pageInfo, new LambdaQueryWrapper<ProductCategory>()
                .eq(ProductCategory::getDeleted, Deleted.NOT_DELETED.getCode()));
        return ResultBean.ok(res.getRecords(), res.getTotal());
    }

    @PostMapping("/get")
    public ResultBean getByCondition(@RequestBody ProductCategoryDTO productCategoryDTO) {
        Integer page = productCategoryDTO.getPage();
        Integer pageSize = productCategoryDTO.getPageSize();
        Page<ProductCategory> pageInfo = new Page<>(page, pageSize);
        Page<ProductCategory> res = categoryService.page(pageInfo, new LambdaQueryWrapper<ProductCategory>()
                .eq(ProductCategory::getDeleted, Deleted.NOT_DELETED.getCode())
                .like(productCategoryDTO.getName() != null, ProductCategory::getName, productCategoryDTO.getName())
        );
        return ResultBean.ok(res.getRecords(), res.getTotal());
    }

    @PostMapping("/save")
    public ResultBean addOrUpdate(@RequestBody ProductCategoryDTO productCategoryDTO) {
        try {

            ProductCategory productCategory = BeanUtil.copyProperties(productCategoryDTO, ProductCategory.class);
            LambdaQueryWrapper<ProductCategory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ProductCategory::getDeleted, Deleted.DELETED.getCode())
                    .eq(productCategory.getName() != null, ProductCategory::getName, productCategory.getName()); ;
            ProductCategory one = categoryService.getOne(queryWrapper);
            if (one != null) {
                categoryService.removeById(one.getId());
            }
            if (productCategory.getId() == null) {
                categoryService.save(productCategory);
                return ResultBean.ok("添加成功");
            } else {
                categoryService.updateById(productCategory);
                return ResultBean.ok("修改成功");
            }
        } catch (Exception e) {
            if (e.getMessage().contains("Duplicate entry") || e.getCause().getMessage().contains("Duplicate entry")) {
                return ResultBean.fail("类别名已存在，请检查重复项");
            }
            return ResultBean.fail("保存失败");
        }

    }

    @DeleteMapping("/delete/{id}")
    public ResultBean delete(@PathVariable("id") Long id) {
        try {
            LambdaUpdateWrapper<ProductCategory> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(ProductCategory::getId, id);
            updateWrapper.set(ProductCategory::getDeleted, Deleted.DELETED.getCode());
            categoryService.update(updateWrapper);
            return ResultBean.ok();
        } catch (Exception e) {
            return ResultBean.fail("删除失败");
        }
    }

}
